package com.mapper;

import com.pojo.Teacher;
import com.pojo.Topic;
import com.pojo.TopicType;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TopicMapper {

    @Select("select * from topic where type_id = #{type_id} and state = 2 and createTime = #{year} limit #{page},#{pagesize}")
    List<Topic> getByTypePage(Integer type_id,String year,Integer page,Integer pagesize);

    @Select("select * from topic where stu_id = #{id}")
    List<Topic> getByStu_id(String  id);


    @Select("select count(*) from topic where type_id = #{type_id} and state = 2 and createTime = #{year}")
    Integer countTopic(Integer type_id,String year);


    @Select("select * from topic where id = #{id}")
    Topic getById(Integer id);

    @Select("select * from topic where state = 0 limit #{page},#{pagesize}")
    List<Topic> getInspect(Integer page,Integer pagesize);


    @Select("select * from topic where teacher_id = #{open_id} order by state,createTime desc limit #{page},#{pagesize}")
    List<Topic> getTeacherAllTopic(String open_id,Integer page,Integer pagesize);

    @Select("select * from topic where teacher_id = #{open_id} and state = 2 limit #{page},#{pagesize}")
    List<Topic> getTeacherApprovedTopic(String open_id,Integer page,Integer pagesize);

    @Select("select * from topic_type")
    List<TopicType> getTypes();

    @Insert("insert into topic_type(name) value(#{name})")
    void addType(String name);

    @Select("select * from topic_type where id = #{id}")
    TopicType getTypeById(Integer id);

    @Update("update topic_type set name = #{name} where id = #{id}")
    void updateType(Integer id,String name);

    @Delete("delete from topic_type where id = #{id}")
    void delType(Integer id);

    @Update("update topic set state = #{state} where id = #{topic_id}")
    void examine(Integer topic_id,Integer state);

    @Insert("insert into topic(type_id,teacher_id,title,conents,img,createTime) value(#{type_id},#{teacher_id},#{title},#{conents},#{img},YEAR(NOW()))")
    void addTopic(Integer type_id,String teacher_id,String title,String conents,String img);

    @Update("update topic set type_id = #{type_id},teacher_id = #{teacher_id},title = #{title},conents = #{conents},img = #{img} where id = #{id}")
    void updateTopic(Integer id,Integer type_id,Integer teacher_id,String title,String conents,String img);

    @Delete("delete from topic where id = #{id}")
    void delTopic(Integer id);

    @Update("update topic set stu_id = #{open_id} ,state=2 where id = #{topic_id}")
    void subscribe(String open_id,Integer topic_id);

    @Select("select * from topic where title like #{like}")
    List<Topic> search(String like);

    @Select("select distinct createTime from topic order by createTime desc")
    List<String> getYears();

    @Select("select name from topic_type where id = #{id}")
    String getTypeNameById(Integer id);


}
